package LeetCode_1_100;

class Solution06 {
    public String convert(String s, int numRows) {
        if (numRows == 1) {
            return s;
        }

        StringBuilder result = new StringBuilder();
        int cycle = 2 * numRows - 2;

        for (int i = 0; i < numRows; i++) {
            for (int j = 0; j + i < s.length(); j += cycle) {
                result.append(s.charAt(j + i));
                if (i != 0 && i != numRows - 1) { // 中间行处理斜列
                    int secondIndex = j + cycle - i;
                    if (secondIndex < s.length()) {
                        result.append(s.charAt(secondIndex));
                    }
                }
            }
        }
        return result.toString();
    }
}

public class LeetCode06_convert {
    public static void main(String[] args) {
        Solution06 solution = new Solution06();
        String s;
        int numRows;

        //Case 1
        s = "PAYPALISHIRING";
        numRows = 3;
        System.out.println(solution.convert(s, numRows));

        //Case 2
        s = "PAYPALISHIRING";
        numRows = 4;
        System.out.println(solution.convert(s, numRows));

        //Case 3
        s = "A";
        numRows = 1;
        System.out.println(solution.convert(s, numRows));
    }
}
